<%@ page language="java" import="java.sql.*,java.util.*" pageEncoding="utf-8"%>
<%request.setCharacterEncoding("UTF-8");%>
<%String id = request.getParameter("stuid");%>
<%String stuName = request.getParameter("stuname");%>
<%String stuBirthday = request.getParameter("stubirthday");%>
<%String stuClass = request.getParameter("stuclass");%>
<html>
<head>
    <title>修改记录</title>

    <!-- 声明变量：接收查询语句返回的结果 -->
    <%! ResultSet rs; %>
    <%
        String dbName = "studb";
        String userName = "root";
        String userPassword = "123456";
        String url = "jdbc:mysql://localhost/"+
                dbName+"?user="+userName
                +"&password="+userPassword;
        //1、加载JDBC驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2、连接数据库
        Connection conn =DriverManager.getConnection(url);
        //3、创建语句对象，用来执行SQL语句
        Statement statement = conn.createStatement();
        //4、定义SQL语句（更新student表哪些记录（满足where条件的记录）的哪些属性(set关键字后面的属性)
        String sql = "update student set stuBirthday=?, stuClass=? where number=?";

        //5、使用PreparedStatement对象包含的SQL语句允许有一个或多个输入参数，创建sql语句时用？代替
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //6、设置第一个？对应的值
        preparedStatement.setString(1, stuBirthday);
        preparedStatement.setString(2, stuClass);
        preparedStatement.setString(3, id);
        int result = preparedStatement.executeUpdate();
        //根据返回结果，判断是否插入成功（result>0则成功;否则添加失败）
        if(result > 0)
            out.print("<script type='text/javascript'>alert('添加成功')</script>");
        else
            out.print("<script type='text/javascript'>alert('添加失败')</script>");
        //6、关闭连接
        //conn.close();
    %>
</head>

<body>
<table border="1">
    <th colspan="4" style="font-size: x-large;alignment: center">学生信息表</th>
    <tr style="text-align: center">
        <td>学号</td>
        <td>姓名</td>
        <td>出生日期</td>
        <td>所在班级</td>
    </tr>
    <%
        //循环遍历rs中数据
        while(rs.next()){
    %>
    <tr style="text-align: center">
        <td><%=rs.getString(1)%></td>
        <td><%=rs.getString(2)%></td>
        <td><%=rs.getString("birthday")%></td>
        <td><%=rs.getString("stuclass")%></td>
    </tr>
    <%}%>

    <%
        //关闭原则：先创建的后关闭
        rs.close();
        statement.close();
        conn.close();
    %>
</table>
</body>
</html>
